import PayType from "@/utils/enum/order/PayType";

/**
 * 发起支付请求 (用于微信小程序)
 * @param {Object} option 参数
 * https://zh.uniapp.dcloud.io/api/plugins/payment.html#%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%94%AF%E4%BB%98
 */
export const weixinMIniProgramPay = (options) => {
  return new Promise((resolve, reject) => {
    uni.requestPayment({
      provider: "wxpay",
      timeStamp: options.timeStamp || "",
      nonceStr: options.nonceStr || "",
      package: "prepay_id=" + options.prepay_id,
      signType: "MD5",
      paySign: options.paySign || "",
      success(res) {
        const option = {
          isRequireQuery: true, // 是否需要主动查单
          outTradeNo: options.out_trade_no, // 交易订单号
          method: "wechat",
        };
        resolve({ res, option });
      },
      fail: (res) => reject(res),
    });
  });
};

/**
 * 微信公众号调用微信支付
 * @param {Object} option 参数
 * https://zh.uniapp.dcloud.io/api/plugins/payment.html#%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%94%AF%E4%BB%98
 */
// #ifdef H5
export const weixinH5pay = (options) => {
  return new Promise((resolve, reject) => {
    WeixinJSBridge.invoke(
      "getBrandWCPayRequest",
      {
        appId: options.appId,
        timeStamp: options.timeStamp,
        nonceStr: options.nonceStr,
        package: options.package,
        signType: options.signType,
        paySign: options.paySign,
      },
      (res) => {
        if (res.err_msg == "get_brand_wcpay_request:ok") {
          const option = {
            isRequireQuery: true, // 是否需要主动查单
            outTradeNo: options.out_trade_no, // 交易订单号
            method: "wechat",
          };
          resolve({ res, option });
        } else {
          reject(res);
        }
      }
    );
  });
};
// #endif
